import router from '@/router'
import { ElMessageBox, } from 'element-plus'
import { accountLogin, getCustomerInfo } from '@/api/login'
import { getSiteInfo } from '@/api/common'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { isHttp, isEmpty } from "@/utils/validate"
// import defAva from '@/assets/images/profile.jpg'

const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      user: {},
      token: getToken(),
      customerUid: '',
      contactMan: '',
      customerName: '',
      headImage: '',
      websiteInfo: {},
      categoryList: [],
      hotSearch: [],
      majorList: [],
      navigateList: [],
      qqZiXun: [],
      // roles: [],
      // permissions: []
    }),
    actions: {
      setTokenValue(token) {
        return new Promise((resolve, reject) => {
          this.token = token
          setToken(token)
          resolve()
        })
      },
      // 登录
      login(params) {

        return new Promise((resolve, reject) => {
          accountLogin(params).then(res => {
            setToken(res.data)
            this.token = res.data
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 获取用户信息
      getInfo() {
        return new Promise((resolve, reject) => {
          getCustomerInfo().then(res => {
            const user = res.data
            // let avatar = user.headImage || ""
            // if (!isHttp(avatar)) {
            //   avatar = (isEmpty(avatar)) ? defAva : import.meta.env.VITE_APP_BASE_API + avatar
            // }
            // if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            //   this.roles = res.roles
            //   this.permissions = res.permissions
            // } else {
            //   this.roles = ['ROLE_DEFAULT']
            // }
            this.user = user
						
			if (user){
				this.customerUid = user.customerUid
				this.contactMan = user.contactMan
				this.customerName = user.customerName
				this.headImage = user.headImage
			}
            

            /* 初始密码提示 */
            // if (res.isDefaultModifyPwd) {
            //   ElMessageBox.confirm('您的密码还是初始密码，请修改密码！', '安全提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
            //     router.push({ name: 'Profile', params: { activeTab: 'resetPwd' } })
            //   }).catch(() => { })
            // }
            /* 过期密码提示 */
            // if (!res.isDefaultModifyPwd && res.isPasswordExpired) {
            //   ElMessageBox.confirm('您的密码已过期，请尽快修改密码！', '安全提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
            //     router.push({ name: 'Profile', params: { activeTab: 'resetPwd' } })
            //   }).catch(() => { })
            // }
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 获取网站信息
      getWebsiteInfo() {
        return new Promise((resolve, reject) => {
          getSiteInfo().then(res => {
            let { data } = res
            this.websiteInfo = data
            this.categoryList = data.categoryList
            this.hotSearch = data.hotSearch
            this.majorList = data.majorList
            this.navigateList = data.navigateList
            this.qqZiXun = data.qqZiXun
            document.title = this.websiteInfo.platformName
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          this.token = ''
          this.customerUid = ''
          removeToken()
          resolve()
        })
      }
    }
  })

export default useUserStore
